package com.qkbb.admin.kuibu.qkbb.citylist;

import android.content.Context;
import android.util.Log;


import com.qkbb.admin.kuibu.qkbb.JavaBean.City;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/2/3.
 */
public class putCtiycode {
    static List<City> list;
    InputStreamReader read;
    InputStream stream;
    BufferedReader br;
    StringBuffer stringBuffer;
    public void getString(Context context){
        list=new ArrayList<City>();
        String str="";
        try {
           stream = context.getAssets().open("city_code.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        stringBuffer=new StringBuffer();
        read=new InputStreamReader(stream);
        br=new BufferedReader(read);
        try {



          while((str=br.readLine())!=null){
              stringBuffer.append(str);
          }

            br.close();
            read.close();
            stream.close();
            Log.e("str", stringBuffer.toString());

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }
        try {
            JSONObject jsonObject=new JSONObject(stringBuffer.toString());
            JSONObject jsonObject1=jsonObject.getJSONObject("plist");
            JSONObject jsonObject2=jsonObject1.getJSONObject("dict");
            JSONArray jsonArray=jsonObject2.getJSONArray("array");
            JSONObject jsonObject3=jsonArray.getJSONObject(0);
            JSONArray jsonArray1=jsonObject3.getJSONArray("dict");
            for(int i=0;i<jsonArray1.length();i++){
                City city=new City();
                JSONObject jsonObject4=jsonArray1.getJSONObject(i);
                JSONArray jsonArray2=jsonObject4.getJSONArray("string");
                city.setCityCode(jsonObject4.getInt("integer"));
                if (jsonObject4.isNull("real")){
                    city.setName(jsonArray2.getString(3));
                    city.setProvince(jsonArray2.getString(5));
                }
                else {
                    city.setProvince(jsonArray2.getString(3));
                    city.setName(jsonArray2.getString(1));
                }
                list.add(city);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    public int getcode(String ctiyname,Context context){
        int citycode = 0;
        if (list==null){
            getString(context);
        }
        for(int i=0;i<list.size()-1;i++){

            if (list.get(i).getName().equals(ctiyname)){
                citycode=list.get(i).getCityCode();
            }
        }
        return citycode;
    }
    public String Getcitynames(int ctiycode,Context context){
        String cityname="";
        if (list==null){
            getString(context);
        }
        for(int i=0;i<list.size()-1;i++){
            if (list.get(i).getCityCode()==ctiycode){
               cityname=list.get(i).getName();
            }
        }

        return cityname;
    }



}


